package net.news.service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import net.news.dao.NewsDao;
import net.news.dao.NewsTypeDao;
import net.news.entity.News;
import net.news.entity.NewsPeople;
import net.news.entity.NewsType;

import org.springframework.stereotype.Service;

import com.liesp.util.PropertyFilter;

/**
 * 项目名称：cmunews3084<br>
 * 类名称：newsService <br>
 * 类描述： TODO(请输入类的描述) <br>
 * 创建人：charles 创建时间：2011-5-8 下午11:10:52 <br>
 * 修改人： 修改时间： <br>
 * 修改备注：
 * 
 * @version V1.0
 */
@Service
public class NewsService {
	@Resource
	private NewsDao newsDao;
	@Resource
	private NewsPeopleService newsPeopleService;
	@Resource
	private NewsTypeService newsTypeService;
	/**@author charles 2011-05-08 23:44
	 * 保存新闻业务方法
	 * @param news 新闻实体
	 * @param newsPeople
	 * @param newsType
	 */
	public void addnews(News news,NewsPeople newsPeople,NewsType newsType){
		NewsPeople nPeople=newsPeopleService.userLogin(newsPeople);
		NewsType nType=newsTypeService.getNewsTypeById(newsType);
		news.setNewsPeople(nPeople);
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String time = df1.format(date);
		Timestamp ts = Timestamp.valueOf(time);
		news.setNewsTime(ts);
		news.setNewsType(nType);
		newsDao.savaNews(news);
	}
	
	/**查询所有新闻列表方法
	 * @return
	 */
	public List<News> getNewsList(){
		List<News> list = newsDao.findNewsList();
		return list;
	}
	
	/**编辑新闻方法
	 * @param news 新闻实体
	 * @return
	 */
	public News getNewsById(News news){
		List<News> newss=newsDao.findNewsListById(news);
		if (null!=newss&&newss.size()>0) {
			News entity=newss.get(0);
			return entity;
		}
		return null;
	}
	
	/**更新新闻
	 * @param news 新闻
	 * @param newsType 新闻类型
	 */
	public void modifyNews(News news,NewsPeople newsPeople,NewsType newsType){
		NewsPeople nPeople=newsPeopleService.userLogin(newsPeople);
		NewsType nType=newsTypeService.getNewsTypeById(newsType);
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String time = df1.format(date);
		Timestamp ts = Timestamp.valueOf(time);
		news.setNewsTime(ts);
		news.setNewsPeople(nPeople);
		news.setNewsType(nType);
		newsDao.merge(news);
	}
	
	/**模糊查询对象
	 * @param filters
	 * @return
	 */
	public List<News> getNewsByFilter(List<PropertyFilter> filters){
		String[] claName=new String[]{"newsPeople","newsType"};
		List<News> newss=newsDao.findNewsQueryOr(filters,claName);
		return newss;
	}
	
	/**首页显示方法
	 * @return
	 */
	public HashMap<String, Object> findNewsIndex(){
		HashMap<String,Object> newsmap=new HashMap<String, Object>();
		List<News> newsDay=newsDao.findNewsByNewsType(1);
		List<News> newsMsg=newsDao.findNewsByNewsType(2);
		List<News> newsHot=newsDao.findNewsHot();
		newsmap.put("newsDay", newsDay);
		newsmap.put("newsMsg", newsMsg);
		newsmap.put("newsHot", newsHot);
		return newsmap;
	}
	
	/**点击新闻查询详细信息，并记录点击率
	 * @param news 新闻实体类
	 * @return
	 */
	public News clickNewsInfo(News news){
		List<News> newss=newsDao.findNewsListById(news);
		if (null!=newss&&newss.size()>0) {
			News entity=newss.get(0);
			entity.setClickRate(newss.get(0).getClickRate()==null?1:newss.get(0).getClickRate()+1);
			newsDao.updateNews(entity);
			return entity;
		}
		return null;
	}
	
	/**根据新闻类型id查询新闻
	 * @param news
	 * @return
	 */
	public List<News> getNewsByNewsType(News news){
		List<News> newss=newsDao.findNewsByNewsType(news.getNewsType().getId());
		return newss;
	}
	
	/**删除新闻方法
	 * @param news
	 */
	public void deleteNews(News news){
		News news2=this.clickNewsInfo(news);
		newsDao.delete(news2);
	}
	
	public List<News> newsHot(){
		List<News> newss=newsDao.findNewsHot();
		return newss;
	}
}
